# Maintainer: Andrew Sun <adsun701@gmail.com>

_realname=ptex
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=2.3.2
pkgrel=1
pkgdesc="Per-Face Texture Mapping for Production Rendering (mingw-w64)"
arch=('any')
url='http://ptex.us/'
license=('APACHE')
makedepends=("${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-doxygen"
             "${MINGW_PACKAGE_PREFIX}-gcc")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-winpthreads-git")
source=("${_realname}-${pkgver}.tar.gz"::"https://github.com/wdas/${_realname}/archive/v${pkgver}.tar.gz"
        "0001-ptex-2.1.28-remove-msc_ver.patch"
        "0002-ptex-2.1.28-install-dll-correctly.patch"
        "0003-fix-export.patch"
        "0004-mingw-isfinite.patch"
        "0010-fallback-version-cmake.patch")
sha256sums=('30aeb85b965ca542a8945b75285cd67d8e207d23dbb57fcfeaab587bb443402b'
            '4970dc32c355f930833fe72be38c570a557071785a726a59245541090d72c806'
            'b0d84847aa647165cc172bdf06968e94fe890b91b8fb7cd24bdf2d545d96306e'
            'bfae302d5a03db0123de8c7da1b2da6f9dacbadd7a67376b038ebaabbcdcbd38'
            '2ffad92e3dae6647a5ae4dc7f4b1ad77297495fbf70825f86ea6490c884822f9'
            'b6bed9c3b0e2f5a52374ba61c6c5dca02b29558b1c5612594f4ac5d89a0b7587')

prepare() {
  cd "${srcdir}"/${_realname}-${pkgver}
  patch -Np1 -i "${srcdir}/0001-ptex-2.1.28-remove-msc_ver.patch"
  patch -Np1 -i "${srcdir}/0002-ptex-2.1.28-install-dll-correctly.patch"
  patch -Np1 -i "${srcdir}/0003-fix-export.patch"
  patch -Np1 -i "${srcdir}/0004-mingw-isfinite.patch"

  # Upstream patches
  patch -Np1 -i "${srcdir}/0010-fallback-version-cmake.patch"

  cd src/doc
  ${MINGW_PREFIX}/bin/doxygen -u
}

build() {
  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  [[ -d "${srcdir}"/build-${CARCH} ]] && rm -rf "${srcdir}"/build-${CARCH}
  mkdir -p "${srcdir}"/build-${CARCH} && cd "${srcdir}"/build-${CARCH}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake \
      -G'MSYS Makefiles' \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      "${extra_config[@]}" \
      -DPTEX_BUILD_SHARED_LIBS=ON \
      -DPTEX_BUILD_STATIC_LIBS=ON \
      ../${_realname}-${pkgver}

  make
}

package() {
  cd "${srcdir}"/build-${CARCH}
  make install DESTDIR="${pkgdir}"
}
